﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using SAC.Helper;
using DAL;
namespace BLL
{
    public class BLLValRep
    {
        static string xzTime = "2012-1-1";     //取值限制时间
        static string toExcel = "yse";         //导出Excel是否控制小数点
        string rtDBType = "";   //实时数据库
        string rlDBType = "";   //关系数据库

        DALRep dr = new DALRep();
        DALReport drp = new DALReport();
        DALValRep dvp = new DALValRep();
        ArrayList list = new ArrayList();
        StringBuilder sb = new StringBuilder();
        StrHelper sh = new StrHelper();
        DALUtilityMehtod um = new DALUtilityMehtod();

        /// <summary>
        /// 初始化函数
        /// </summary>
        private void init()
        {
            if (IniHelper.ReadIniData("Report", "XZTime", null) != "")
                xzTime = IniHelper.ReadIniData("Report", "XZTime", null);

            if (IniHelper.ReadIniData("Report", "Excle", null) != "")
                toExcel = IniHelper.ReadIniData("Report", "Excle", null);
        }


        public string RetTableByVal(string repName, string date, string unit, string repType)
        {
            this.init();

            string sql = "";
            string qsrq = "";
            string jsrq = "";
            string bzID = "";
            string bName = "";
            string arrBZ = null;

            DateTime dtNow = DateTime.Now;
            bool isTrue = false;

            if (repType == "zr")
            {
                bName = "统计数据值日表";

                if (dtNow.Year == DateTime.Parse(date).Year && dtNow.Month == DateTime.Parse(date).Month && dtNow.Day == DateTime.Parse(date).Day)
                {
                    qsrq = DateTime.Parse(date).ToString("yyyy-MM-dd 0:00:00");
                    jsrq = dtNow.ToString("yyyy-MM-dd H:00:00"); isTrue = true;
                }
                else
                {
                    qsrq = DateTime.Parse(date).ToString("yyyy-MM-dd 0:00:00");
                    jsrq = DateTime.Parse(date).ToString("yyyy-MM-dd 23:59:59"); isTrue = false;
                }
            }
            else if (repType == "zy")
            {
                bName = "统计数据值月表";

                if (dtNow.Year == DateTime.Parse(date).Year && dtNow.Month == DateTime.Parse(date).Month)
                {
                    qsrq = DateTime.Parse(date).ToString("yyyy-MM-01 0:00:00");
                    jsrq = dtNow.AddDays(-1).ToString("yyyy-MM-dd 23:59:59"); isTrue = true;
                }
                else
                {
                    qsrq = DateTime.Parse(date).ToString("yyyy-MM-01 0:00:00");
                    jsrq = sh.LastDayOfMonth(DateTime.Parse(date)).ToString("yyyy-MM-dd 23:59:59"); isTrue = false;
                }
            }

            ArrayList list = dvp.RetArrsRepValDay(repType, repName, qsrq, jsrq, bName, unit, out bzID);

            string[] zb = (string[])list[0];
            string[] un = (string[])list[1];
            string[] lay = (string[])list[2];
            string[] dv = (string[])list[3];
            string[] dvExcel = (string[])list[4];

            #region 报表
            if (toExcel == "yes")
                sb.Append("<table width=\"98%\" border=\"1\" align=\"center\" class=\"bg\" valign=\"top\" bordercolorlight=\"#999999\" bordercolordark=\"#FFFFFF\" cellspacing=\"0\" cellpadding=\"1\" id=\"CX\">");
            else
                sb.Append("<table width=\"98%\" border=\"1\" align=\"center\" class=\"bg\" valign=\"top\" bordercolorlight=\"#999999\" bordercolordark=\"#FFFFFF\" cellspacing=\"0\" cellpadding=\"1\" id=\"CX1\">");

            if (repType == "zr")
            {
                sb.Append("<tr height='35px' bgcolor=\"#CFE6FC\"><td colspan=\"" + (4 + bzID.Split(',').Length - 1) + "\">" + repName + "日报表</td></tr>");
                sb.Append("<tr height='20px' bgcolor=\"#CFE6FC\" align=\"right\"><td colspan='" + (4 + bzID.Split(',').Length - 1) + "'>统计日期：" + date + "</td></tr>");
                sb.Append("<tr style=\"width: 80%;\" bgcolor=\"#CFE6FC\"> ");
                sb.Append("<td style=\"width: 20%;\"><div align=\"center\">指 标 名 称</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">单 位</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">当 日</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">环 比</td>");

                ArrayList listOld = drp.RetArrsRepDay(repName, "日点", Convert.ToDateTime(qsrq).AddDays(-1).ToString(), Convert.ToDateTime(jsrq).AddDays(-1).ToString(), isTrue);
                ArrayList listDay = drp.RetArrsRepDay(repName, "日点", qsrq, jsrq, isTrue);


                string[] bz = bzID.Split(',');
                for (int i = 0; i < bz.Length - 1; i++)
                {
                    string bzs = getValueName(bz[i]);

                    sb.AppendFormat("<td bgcolor=\"#CFE6FC\" align='center'>{0}</td>", bzs);
                }

                sb.Append("</tr>");

                for (int i = 1; i < zb.Length; i++)
                {
                    sb.AppendFormat("<tr><td bgcolor=\"#CFE6FC\" align='center'>{0}</td>", zb[i]);
                    sb.AppendFormat("<td bgcolor=\"#CFE6FC\" align='center'>{0}</td>", un[i]);

                    if (((string[])listDay[3])[i] != null)
                    {
                        double valuenew = Convert.ToDouble(((string[])listDay[3])[i].ToString().Split(',')[0]);
                        double valueold = Convert.ToDouble(((string[])listOld[3])[i].ToString().Split(',')[0]);
                        double values = valuenew - valueold;

                        values = getDouble(Convert.ToDouble(values), 2);

                        sb.AppendFormat("<td align='center'>{0}</td>", valuenew);

                        sb.AppendFormat("<td align='center'>{0}</td>", values);
                    }
                    else
                    {
                        sb.Append("<td align='center'>&nbsp;</td>");

                        sb.Append("<td align='center'>&nbsp;</td>");
                    }

                    if (dv[i] != null)
                    {
                        string[] str = dv[i].Split(',');

                        for (int k = 0; k < str.Length - 1; k++)
                        {
                            sb.AppendFormat("<td align='center'>{0}</td>", str[k]);
                        }
                    }
                    else
                    {
                        for (int k = 0; k < 3; k++)
                        {
                            sb.AppendFormat("<td align='center'>{0}</td>", "&nbsp;");
                        }
                    }

                    sb.Append("</tr>");

                }
            }
            else if (repType == "zy")
            {
                sb.Append("<tr height='35px' bgcolor=\"#CFE6FC\"><td colspan='" + (zb.Length + 1) + "'>" + repName + "</td></tr>");
                sb.Append("<tr height='20px' bgcolor=\"#CFE6FC\" align=\"right\"><td colspan='" + (zb.Length + 1) + "'>统计日期：" + date + "</td></tr>");
                sb.Append("<tr style=\"width: 80%;\" bgcolor=\"#CFE6FC\"> ");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">时  间</td>");
                sb.Append("<td style=\"width: 20%;\"><div align=\"center\">指 标 名 称</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">单  位</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">年累计</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">月累计</td>");

                ArrayList listYear = drp.RetArrsRepMonth(repName, "月报", "月点", Convert.ToDateTime(qsrq).Year + "-" + "01-01 0:00:00.0", jsrq, Convert.ToDateTime(qsrq).Year + "-" + "01-01 0:00:00.0", (Convert.ToDateTime(qsrq).Year + 1) + "-" + "01-01 0:00:00.0", isTrue);

                if (Convert.ToDateTime(date).Year == DateTime.Now.Year)
                    isTrue = true;
                else
                    isTrue = false;

                ArrayList listMonth = drp.RetArrsRepMonth(repName, "月报", "月点", qsrq, jsrq, qsrq, Convert.ToDateTime(qsrq).AddMonths(1).ToShortDateString(), isTrue);

                string[] bz = bzID.Split(',');
                for (int i = 0; i < bz.Length - 1; i++)
                {
                    string bzs = getValueName(bz[i]);
                    sb.AppendFormat("<td bgcolor=\"#CFE6FC\" align='center'>{0}</td>", bzs + "月累计");
                }

                sb.Append("</tr>");

                for (int i = 1; i < zb.Length; i++)
                {
                    sb.AppendFormat("<tr><td bgcolor=\"#CFE6FC\" align='center'>{0}</td>", zb[i]);
                    sb.AppendFormat("<td bgcolor=\"#CFE6FC\" align='center'>{0}</td>", un[i]);

                    #region 年累计
                    string valueYear = ((string[])listYear[3])[i].ToString().Split(',')[0];

                    if (((string[])listYear[3])[i] != null)
                    {
                        double value = getDouble(Convert.ToDouble(valueYear), 2);

                        sb.AppendFormat("<td align='center'>{0}</td>", value);
                    }
                    else
                    {
                        sb.Append("<td align='center'>&nbsp;</td>");
                    }
                    #endregion

                    #region 月累计
                    string valueMonth = ((string[])listMonth[3])[i].ToString().Split(',')[0];
                    if (((string[])listMonth[3])[i] != null)
                    {
                        double value = getDouble(Convert.ToDouble(valueMonth), 2);

                        sb.AppendFormat("<td align='center'>{0}</td>", value);
                    }
                    else
                    {
                        sb.Append("<td align='center'>&nbsp;</td>");
                    }
                    #endregion

                    #region 月值
                    string[] str = dv[i].Split(',');

                    for (int k = 0; k < str.Length - 1; k++)
                    {
                        sb.AppendFormat("<td align='center'>{0}</td>", str[k]);
                    }
                    #endregion

                    sb.Append("</tr>");

                }
            }
            sb.Append("</table>");
            #endregion

            #region 导出Excel表
            if (toExcel == "no")
                sb.Append("<table width=\"90%\" border=\"1\" align=\"center\" class=\"bg\" valign=\"top\" bordercolorlight=\"#999999\" bordercolordark=\"#FFFFFF\" cellspacing=\"0\" cellpadding=\"1\" id=\"CX\" style=\"display:none;\">");
            else
                sb.Append("<table width=\"90%\" border=\"1\" align=\"center\" class=\"bg\" valign=\"top\" bordercolorlight=\"#999999\" bordercolordark=\"#FFFFFF\" cellspacing=\"0\" cellpadding=\"1\" id=\"CX1\" style=\"display:none;\">");

            if (repType == "zr")
            {
                sb.Append("<tr height='35px' bgcolor=\"#CFE6FC\"><td colspan=\"" + (4 + bzID.Split(',').Length - 1) + "\">" + repName + "日报表</td></tr>");
                sb.Append("<tr height='20px' bgcolor=\"#CFE6FC\" align=\"right\"><td colspan='" + (4 + bzID.Split(',').Length - 1) + "'>统计日期：" + date + "</td></tr>");
                sb.Append("<tr style=\"width: 80%;\" bgcolor=\"#CFE6FC\"> ");
                sb.Append("<td style=\"width: 20%;\"><div align=\"center\">指 标 名 称</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">单 位</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">当 日</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">环 比</td>");

                ArrayList listOld = drp.RetArrsRepDay(repName, "日点", Convert.ToDateTime(qsrq).AddDays(-1).ToString(), Convert.ToDateTime(jsrq).AddDays(-1).ToString(), isTrue);
                ArrayList listDay = drp.RetArrsRepDay(repName, "日点", qsrq, jsrq, isTrue);


                string[] bz = bzID.Split(',');
                for (int i = 0; i < bz.Length - 1; i++)
                {
                    string bzs = getValueName(bz[i]);

                    sb.AppendFormat("<td bgcolor=\"#CFE6FC\" align='center'>{0}</td>", bzs);
                }

                sb.Append("</tr>");

                for (int i = 1; i < zb.Length; i++)
                {
                    sb.AppendFormat("<tr><td bgcolor=\"#CFE6FC\" align='center'>{0}</td>", zb[i]);
                    sb.AppendFormat("<td bgcolor=\"#CFE6FC\" align='center'>{0}</td>", un[i]);

                    if (((string[])listDay[3])[i] != null)
                    {
                        double valuenew = Convert.ToDouble(((string[])listDay[3])[i].ToString().Split(',')[0]);
                        double valueold = Convert.ToDouble(((string[])listOld[3])[i].ToString().Split(',')[0]);
                        double values = valuenew - valueold;

                        values = getDouble(Convert.ToDouble(values), 2);

                        sb.AppendFormat("<td align='center'>{0}</td>", valuenew);

                        sb.AppendFormat("<td align='center'>{0}</td>", values);
                    }
                    else
                    {
                        sb.Append("<td align='center'>&nbsp;</td>");

                        sb.Append("<td align='center'>&nbsp;</td>");
                    }

                    if (dv[i] != null)
                    {
                        string[] str = dv[i].Split(',');

                        for (int k = 0; k < str.Length - 1; k++)
                        {
                            sb.AppendFormat("<td align='center'>{0}</td>", str[k]);
                        }
                    }
                    else
                    {
                        for (int k = 0; k < 3; k++)
                        {
                            sb.AppendFormat("<td align='center'>{0}</td>", "&nbsp;");
                        }
                    }

                    sb.Append("</tr>");

                }
            }
            else if (repType == "zy")
            {
                sb.Append("<tr height='35px' bgcolor=\"#CFE6FC\"><td colspan='" + (zb.Length + 1) + "'>" + repName + "</td></tr>");
                sb.Append("<tr height='20px' bgcolor=\"#CFE6FC\" align=\"right\"><td colspan='" + (zb.Length + 1) + "'>统计日期：" + date + "</td></tr>");
                sb.Append("<tr style=\"width: 80%;\" bgcolor=\"#CFE6FC\"> ");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">时  间</td>");
                sb.Append("<td style=\"width: 20%;\"><div align=\"center\">指 标 名 称</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">单  位</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">年累计</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">月累计</td>");

                ArrayList listYear = drp.RetArrsRepMonth(repName, "月报", "月点", Convert.ToDateTime(qsrq).Year + "-" + "01-01 0:00:00.0", jsrq, Convert.ToDateTime(qsrq).Year + "-" + "01-01 0:00:00.0", (Convert.ToDateTime(qsrq).Year + 1) + "-" + "01-01 0:00:00.0", isTrue);

                if (Convert.ToDateTime(date).Year == DateTime.Now.Year)
                    isTrue = true;
                else
                    isTrue = false;

                ArrayList listMonth = drp.RetArrsRepMonth(repName, "月报", "月点", qsrq, jsrq, qsrq, Convert.ToDateTime(qsrq).AddMonths(1).ToShortDateString(), isTrue);

                string[] bz = bzID.Split(',');
                for (int i = 0; i < bz.Length - 1; i++)
                {
                    string bzs = getValueName(bz[i]);
                    sb.AppendFormat("<td bgcolor=\"#CFE6FC\" align='center'>{0}</td>", bzs + "月累计");
                }

                sb.Append("</tr>");

                for (int i = 1; i < zb.Length; i++)
                {
                    sb.AppendFormat("<tr><td bgcolor=\"#CFE6FC\" align='center'>{0}</td>", zb[i]);
                    sb.AppendFormat("<td bgcolor=\"#CFE6FC\" align='center'>{0}</td>", un[i]);

                    #region 年累计
                    string valueYear = ((string[])listYear[3])[i].ToString().Split(',')[0];

                    if (((string[])listYear[3])[i] != null)
                    {
                        double value = getDouble(Convert.ToDouble(valueYear), 2);

                        sb.AppendFormat("<td align='center'>{0}</td>", value);
                    }
                    else
                    {
                        sb.Append("<td align='center'>&nbsp;</td>");
                    }
                    #endregion

                    #region 月累计
                    string valueMonth = ((string[])listMonth[3])[i].ToString().Split(',')[0];
                    if (((string[])listMonth[3])[i] != null)
                    {
                        double value = getDouble(Convert.ToDouble(valueMonth), 2);

                        sb.AppendFormat("<td align='center'>{0}</td>", value);
                    }
                    else
                    {
                        sb.Append("<td align='center'>&nbsp;</td>");
                    }
                    #endregion

                    #region 月值
                    string[] str = dv[i].Split(',');

                    for (int k = 0; k < str.Length - 1; k++)
                    {
                        sb.AppendFormat("<td align='center'>{0}</td>", str[k]);
                    }
                    #endregion

                    sb.Append("</tr>");

                }
            }
            sb.Append("</table>");
            #endregion

            return sb.ToString();
        }

        #region 班值名称
        private static string getValueName(string id)
        {
            string name = "";
            if (id == "1")
            {
                name = "一值";
            }
            else if (id == "2")
            {
                name = "二值";
            }
            else if (id == "3")
            {
                name = "三值";
            }
            else if (id == "4")
            {
                name = "四值";
            }
            else if (id == "5")
            {
                name = "五值";
            }
            else if (id == "6")
            {
                name = "六值";
            }
            else if (id == "7")
            {
                name = "七值";
            }
            return name;
        }
        #endregion

        #region 四舍五入
        /// <summary>
        /// 四舍五入
        /// </summary>
        /// <param name="result">要转换的数值</param>
        /// <param name="num">保留位数</param>
        /// <returns></returns>
        public double getDouble(double result, int num)
        {
            string res = result.ToString();
            string results = "";
            int index = res.IndexOf('.');

            if (index > 0)
            {
                index += num;
                res += "0000000000000000";
                res = res.Remove(0, index + 1);
                results = result + "0000000000000000000";
                results = results.Substring(0, index + 1);
                res = res.Substring(0, 1);

                string point = "0.";

                for (int count = 0; count < num - 1; count++)
                {
                    point += "0";
                }
                point += "1";


                if (Convert.ToDouble(res) > 4)
                {
                    results = (Convert.ToDouble(results) + Convert.ToDouble(point)).ToString();
                    res = results;
                }
                else
                {
                    res = results;
                }
            }
            else
            {
                res += ".";
                for (int i = 0; i < num; i++)
                {
                    res += "0";
                }
            }
            return Convert.ToDouble(res);
        }
        #endregion

    }
}
